package edu.sctu.jsoup.service;


import edu.sctu.jsoup.entity.Module3Entity;
import edu.sctu.jsoup.repo.Module3Repo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.sql.Blob;

@Service
public class Module3Service {

    @Autowired
    private Module3Repo module3Repo;


    public void run() {

        Module3Entity module3;

        //使用for循环遍历每个网站
        for (int i = (int)module3Repo.count()+1; i <= 6026; i++) {

            module3 = new Module3Entity();

            module3.setHtml_url("https://www.tripadvisor.cn/TourismBlog-t" + i + ".html");

            try {
                Document document = Jsoup.connect(module3.getHtml_url()).get();


                if (document.select("div.not-content").first() == null){

                    module3.setHtml_content(document.toString());
                }else {
                    module3.setHtml_content("无游记"+document.toString());
                }


                module3.setCreate_time(module3.getCreate_time());

                module3Repo.save(module3);

            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("出现异常");
            }


        }


    }
}
